package com.yztc.administrator.meishi;

/*
 * @author: ZTC
 * @Description: $ 解析json
 * @创建时间:  2016/9/21 16:04 
 */

import com.yztc.administrator.meishi.bean.MeiPai;
import com.yztc.administrator.meishi.utils.Httputils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class JsonGet {
    public static List<MeiPai> jsonMedia(String path) {
        byte[] bytes = Httputils.getpath(path);
        String json = new String(bytes);
        List<MeiPai> list = new ArrayList<>();
        try {
            JSONArray array = new JSONArray(json);
            for (int i = 0; i < array.length(); i++) {
                JSONObject obj = array.getJSONObject(i);
                String type = obj.getString("type");
                if (type.equals("media")) {
                    String recommend_caption = obj.getString("recommend_caption");
                    String recommend_cover_pic = obj.getString("recommend_cover_pic");
                    String media = obj.getString("media");
                    JSONObject jsonObject = new JSONObject(media);
                    String caption = jsonObject.getString("caption");
                    String video = jsonObject.getString("video");
                    String likes_count = jsonObject.getString("likes_count");
                    String user = jsonObject.getString("user");
                    JSONObject object = new JSONObject(user);
                    String screen_name = object.getString("screen_name");
                    String avatar = object.getString("avatar");
                    MeiPai meiPai = new MeiPai(avatar, caption, likes_count, recommend_caption, recommend_cover_pic, screen_name, video);
                    list.add(meiPai);
                }
            }
            return list;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}
